package com.lt.flashcard.view;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.Listitem;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Content;
import com.lt.flashcard.model.entity.ContentCategory;
import com.lt.flashcard.model.service.ContentService;
import com.lt.flashcard.model.service.Repository;

public class AdminContentVm {
	
	private List<Content> contents;
	private ContentCategory category;
	private Content content;
	private List<ContentCategory> catgories = null;
	private String selectedid = "";

	@Init
	public void init() {
		Utility.adminLoginOrAccessDenied(false);
		loadContents();
	}
	
	private void loadContents() {
		if(getCategory() == null) {
			ContentService srv = Repository.getInstance().getContentService();
			setContents(srv.getAllContents());
		}
		else {
			setContents(getCategory().getContents());
		}
		
	}

	public List<Content> getContents() {
		return contents;
	}

	public void setContents(List<Content> contents) {
		this.contents = contents;
	}

	public ContentCategory getCategory() {
		return category;
	}

	public void setCategory(ContentCategory category) {
		this.category = category;
	}
	
	@NotifyChange("contents")
	@Command
	public void delete(@BindingParam("id") String id) {
		if(StringUtils.isNotBlank(id)) {
			ContentService srv = Repository.getInstance().getContentService();
			srv.deleteContent(srv.getContent(id));
			loadContents();
		}
	}
	
	@NotifyChange("content")
	@Command
	public void edit(@BindingParam("id") String id) {
		if(StringUtils.isNotBlank(id)) {
			ContentService srv = Repository.getInstance().getContentService();
			content = srv.getContent(id);
		}
	}
	
	@NotifyChange({"contents", "content"})
	@Command
	public void save() {
		if(content != null) {
			ContentService srv = Repository.getInstance().getContentService();
			if(StringUtils.isNotBlank(selectedid)) {
				content.setCategory(srv.getCategory(selectedid));
			}
			srv.saveContent(content);
			loadContents();
			
			content = null;
			selectedid = "";
		}
	}
	
	@NotifyChange("content")
	@Command
	public void newcontent() {
		content = new Content();
	}
	
	@NotifyChange("content")
	@Command
	public void cancel() {
		content = null;
	}
	
	@Command
	public void selectcat(@BindingParam("item") Listitem item) {
		if(item != null) {
			selectedid = ((ObjectId) item.getValue()).toString();
		}
	}

	public Content getContent() {
		return content;
	}

	public void setContent(Content content) {
		this.content = content;
	}

	public List<ContentCategory> getCatgories() {
		if(catgories == null) {
			catgories = new ArrayList<ContentCategory>();
			ContentService srv = Repository.getInstance().getContentService();
			List<ContentCategory> cats = srv.getAllCategories();
			for(ContentCategory cat : cats) {
				ContentCategory nc = new ContentCategory();
				nc.setName(cat.getName());
				nc.setId(cat.getId());
				catgories.add(nc);
				
				for(ContentCategory child : cat.getChild()) {
					ContentCategory nchild = new ContentCategory();
					nchild.setName("-- " + child.getName());
					nchild.setId(child.getId());
					catgories.add(nchild);
				}
			}
		}
		return catgories;
	}
}
